
#include "adc.h"
#include "application.h"
#include "bt.h"
#include "dmac.h"
#include "hal_adc.h"
#include "interrupt.h"
#include "timer3.h"
#include "valve.h"

/**
 * @brief 	NVIC 中断使能
 * @param   enIrq          中断号枚举类型
 * @param   enLevel        中断优先级枚举类型
 * @param	bEn            中断开关
 */
void EnableNvic(IRQn_Type enIrq, en_irq_level_t enLevel, boolean_t bEn)
{
    NVIC_ClearPendingIRQ(enIrq);
    NVIC_SetPriority(enIrq, enLevel);
    if (TRUE == bEn)
    {
        NVIC_EnableIRQ(enIrq);
    }
    else
    {
        NVIC_DisableIRQ(enIrq);
    }
}

void Dmac_IRQHandler(void)
{
    if (DmaTransferComplete == Dma_GetStat(DmaCh1))
    {
    }
    Dma_ClrStat(DmaCh1);
}

void TIM0_IRQHandler(void)
{
    // Timer0 溢出中断
    if (TRUE == Bt_GetIntFlag(TIM0, BtUevIrq))
    {
        Adc_SQR_Start();

        /* K15信号 */
        unsigned short data = adc_data[3];
        if (data > 2000)
        {
            power_up_count = 0;
            power_down_count++;
            if (power_down_count > 10000)
            {
                power_down_count = 10000;
            }
        }
        else if (data < 1000)
        {
            power_up_count++;
            power_down_count = 0;
            if (power_up_count > 10000)
            {
                power_up_count = 10000;
            }
        }

        /* 模式判断 */
        data = adc_data[4];
        if (data > 3500)
        {
            mode_button_press_count = 0;
            mode_button_release_count++;
            if (mode_button_release_count > 10000)
            {
                mode_button_release_count = 10000;
            }
        }
        else if ((data < 3500) && (data > 1000))
        {
            mode_button_press_count++;
            mode_button_release_count = 0;
            if (mode_button_press_count > 10000)
            {
                mode_button_press_count = 10000;
            }
        }

        /* 司机按钮 */
        data = adc_data[5];
        if (data < 1000)
        {
            driver_button_press_count++;
            driver_button_release_count = 0;
            if (driver_button_press_count > 10000)
            {
                driver_button_press_count = 10000;
            }
        }
        else if (data > 2000)
        {
            driver_button_release_count++;
            driver_button_press_count = 0;
            if (driver_button_release_count > 10000)
            {
                driver_button_release_count = 10000;
            }
        }

        /* 10ms 标志位 */
        task_1ms_flg = 1;

        Bt_ClearIntFlag(TIM0, BtUevIrq);
    }
}

/**
 * @brief	Timer1 中断函数标志位，用于处理反吹过程阀的控制
 */
void TIM1_IRQHandler(void)
{
    if (TRUE == Bt_GetIntFlag(TIM1, BtUevIrq))
    {
        if (controller.blow_mode == auto_mode)
        {
            if (blow_back.auto_blow_command == true && blow_back.process == none)
            {
                blow_back.process = blow_delay;
            }
            else if (blow_back.auto_blow_command == false)
            {
                blow_back.process = none;
            }

            auto_blow_back_process();
        }
        else if (controller.blow_mode == manual_mode)
        {
            manual_blow_back_process();
        }

        Bt_ClearIntFlag(TIM1, BtUevIrq);
    }
}

void ADC_IRQHandler(void)
{
    /* ADC转换完成中断 */
    if (TRUE == Adc_GetIrqStatus(AdcMskIrqSqr))
    {
        adc_data[0] = Adc_GetSqrResult(AdcSQRCH0MUX);
        adc_data[1] = Adc_GetSqrResult(AdcSQRCH1MUX);
        adc_data[2] = Adc_GetSqrResult(AdcSQRCH2MUX);
        adc_data[3] = Adc_GetSqrResult(AdcSQRCH3MUX);
        adc_data[4] = Adc_GetSqrResult(AdcSQRCH4MUX);
        adc_data[5] = Adc_GetSqrResult(AdcSQRCH5MUX);
        adc_data[6] = Adc_GetSqrResult(AdcSQRCH6MUX);
        // Adc_SQR_Stop();

        Adc_ClrIrqStatus(AdcMskIrqSqr);
    }
}
